package pers.lance.test;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import pers.lance.xml.db.bean.Student;
import pers.lance.xml.db.dao.BasicDao;
import pers.lance.xml.db.dao.StudentDaoImp;
import pers.lance.xml.db.util.DBUtil;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Lance Li
 * @date 7/10/2018 9:58 PM
 * @desc
 */
public class testStudentDao {
    static BasicDao studentDao;

    @BeforeClass
    public static void init() {
        studentDao = new StudentDaoImp();
    }

    @Test
    public void testConn() {
        Connection conn = DBUtil.getConnection();
        String classname = conn.getClass().getName();
        boolean flag = false;
        if (classname.equals("oracle.jdbc.driver.T4CConnection")) {
            flag = true;
        }
        Assert.assertTrue(flag);
    }

    @Test
    public void testFinAllBooks() {
        Assert.assertTrue(studentDao.findAll() != null);
    }

    @Test
    public void testAddBook() {
        Student student = new Student();
        student.setId(1000001);
        student.setSex("男");
        student.setSchool("scoolset");
        student.setClasses("classs1000001");
        student.setAge(10);
        student.setName("VMJAVA");
        Assert.assertTrue(studentDao.add(student) > 0);
    }

    @Test
    public void testUpdateBook() {
        Student student = new Student();
        student.setId(1000001);
        student.setSex("男");
        student.setSchool("scoolset");
        student.setClasses("classs1000001");
        student.setAge(100);
        student.setName("VMJAVA");
        Assert.assertTrue(studentDao.update(student) > 0);
    }

    @Test
    public void testDeleteBook() {
        Student student = new Student();
        student.setId(1000001);
        student.setSex("男");
        student.setSchool("scoolset");
        student.setClasses("classs1000001");
        student.setAge(10);
        student.setName("VMJAVA");
        Assert.assertTrue(studentDao.delete(student)> 0);
    }

    @Test
    public void testLoadBook() {
        Student student = new Student();
        student.setId(100);
        student.setSex("男");
        student.setSchool("scoolset");
        student.setClasses("classs1000001");
        student.setAge(10);
        student.setName("VMJAVA");
        Assert.assertTrue(studentDao.load(student.getId()) != null);
        Assert.assertTrue(studentDao.load(100000054)== null);
        ;
    }

    @Test
    public void testAddBooks() {
        List<Student> students = new ArrayList<Student>();
        for (int i = 0; i < 5; i++) {
            Student student = new Student();
            student.setId(100);
            student.setSex("男");
            student.setSchool("scoolset");
            student.setClasses("classs1000001");
            student.setAge(10);
            student.setName("VMJAVA");
            students.add(student);
        }
        studentDao.add(students);
    }
}
